package org.hugoduncan.appam.servlet;

import java.io.File;
import java.util.Map;

import org.apache.abdera.xpath.XPath;
import org.codehaus.cargo.container.ContainerType;
import org.codehaus.cargo.container.EmbeddedLocalContainer;
import org.codehaus.cargo.container.InstalledLocalContainer;
import org.codehaus.cargo.container.configuration.ConfigurationType;
import org.codehaus.cargo.container.configuration.LocalConfiguration;
import org.codehaus.cargo.container.deployable.Deployable;
import org.codehaus.cargo.container.deployable.DeployableType;
import org.codehaus.cargo.container.property.GeneralPropertySet;
import org.codehaus.cargo.generic.DefaultContainerFactory;
import org.codehaus.cargo.generic.configuration.ConfigurationFactory;
import org.codehaus.cargo.generic.configuration.DefaultConfigurationFactory;
import org.codehaus.cargo.generic.deployable.DefaultDeployableFactory;
import org.codehaus.cargo.util.log.SimpleLogger;

import junit.extensions.TestSetup;
import junit.framework.Test;

public class ContainerTestSetup extends TestSetup
{
    InstalledLocalContainer container;

    public ContainerTestSetup(Test test) {
        super(test);
    }


    @Override
    public void setUp() throws Exception
    {
        super.setUp();

        if (false)
        {

            // start app server
            Deployable war = new DefaultDeployableFactory().createDeployable(
                "tomcat5x",
                "../testserver/target/testserver.war",
                DeployableType.WAR);

            ConfigurationFactory configurationFactory =
                new DefaultConfigurationFactory();
            LocalConfiguration configuration =
                (LocalConfiguration) configurationFactory.createConfiguration(
                    "tomcat5x", ConfigurationType.STANDALONE);
            configuration.addDeployable(war);
            configuration.setLogger(new SimpleLogger());
            configuration.setProperty(GeneralPropertySet.LOGGING, "high");


            InstalledLocalContainer container = (InstalledLocalContainer)
                new DefaultContainerFactory().createContainer(
                    "tomcat5x", ContainerType.INSTALLED, configuration);

            container.setHome(
                new File("c:/Program Files/Apache Software Foundation/Tomcat 5.5"));
            //container.setOutput(new File("target/output.log"));

            container.start();
        }

        if (true)
        {

            // start app server
            Deployable war = new DefaultDeployableFactory().createDeployable(
                "jetty6x",
                "../testserver/target/testserver.war",
                DeployableType.WAR);

            ConfigurationFactory configurationFactory =
                new DefaultConfigurationFactory();
            LocalConfiguration configuration =
                (LocalConfiguration) configurationFactory.createConfiguration(
                    "jetty6x", ConfigurationType.STANDALONE);
            configuration.addDeployable(war);
            configuration.setLogger(new SimpleLogger());
            configuration.setProperty(GeneralPropertySet.LOGGING, "high");


            EmbeddedLocalContainer container = (EmbeddedLocalContainer)
                new DefaultContainerFactory().createContainer(
                    "jetty6x", ContainerType.EMBEDDED, configuration);
            container.start();
        }

    }

    @Override
    protected void tearDown() throws Exception {
        if (container!=null)
            container.stop();
        super.tearDown();
    }
}
